package com.globalwave.common.web;

import java.util.List;
import java.util.Set;

import com.globalwave.base.BaseAction;
import com.globalwave.common.ArrayPageList;
import com.globalwave.common.Convertor;
import com.globalwave.common.db.ConnectionProvider;
import com.globalwave.common.entity.Field;
import com.globalwave.common.entity.View;
import com.globalwave.common.entity.ViewCriterion;
import com.globalwave.common.entity.VirtualGrid;
import com.globalwave.common.service.ViewService;
import com.opensymphony.xwork2.Preparable;

public class ViewAction extends BaseAction implements Preparable {

    private static final long serialVersionUID = 8582051849254108462L;
    
    protected ViewService viewService ;
    protected View view ;
    protected String sql ;
    protected Long view_id ;
    protected String view_code ;
    protected boolean isGenCode = true ;

	private ViewCriterion criterion ; 

    
    public String execute() throws Exception {    
        return this.list();    
    }
    
    public String list() throws Exception {      
        
        ArrayPageList<View> pageList = 
        	viewService.query(criterion,pageIndex,pageSize,null) ;

        renderXML(pageList.asXML()) ;
        
        return null ;    
        
    }
    
    public String create()  throws Exception {              
        
    	VirtualGrid newView = viewService.create(paramsAsXml, isGenCode) ;
        //Element element = this.getSuccessCreateMessage(null) ;
        
        //element.addElement("view_id").addText(newView.toString()) ;
        
        renderXML(newView.asXML("已经成功保存！")) ;
        
        return null;    
        
    }
    
    public String update()  throws Exception {     
        
        viewService.update(paramsAsXml, isGenCode) ;

        renderXML(this.getSuccessUpdateMessage(null).asXML()) ;
        
        return null;    
        
    }
    
    public String delete()  throws Exception {
        if (this.ids == null) { 
            viewService.delete(view) ;
        } else {
            viewService.deleteAll(this.ids) ;
        }

        renderXML(this.getSuccessDeleteMessage().asXML()) ;
        
        return null;    
        
    }
    
    public String getUIMetaData() throws Exception {

        renderXML(viewService.getUIMetaData(view_id, view_code).asXML()) ;
    	return null ;
    }
    
    
    public String getEditFormMetaData() throws Exception {

        renderXML(viewService.getEditFormMetaData(view_code).asXML()) ;
    	return null ;
    }
    
    public String getFieldsBySql() throws Exception {
    	
        List<Field> fields = viewService.getFieldsBySql(sql) ; 

        renderXML("<root>" + Convertor.list2Xml(fields,"fields").asXML() + "</root>") ;
        
        return null;    
    }

    public String getDbNames() throws Exception {
    	
        Set<String> dbNames = ConnectionProvider.getDbNames() ; 

        renderXML("<root>" + Convertor.set2Options(dbNames, "dbNames").asXML() + "</root>") ;
        
        return null;    
    }


    public String getTables() throws Exception {

        List<View> views = viewService.getTables(view_code) ; 

        renderXML("<root>" + Convertor.list2Xml(views, "views").asXML() + "</root>") ;
        
        return null;    
    }
    

    
    public String sync() throws Exception {
/*
    	SyncControlService syncControlService = 
    		(SyncControlService)WebApplicationContextUtils.getWebApplicationContext(
    				this.getServletContext()).getBean("syncControlService") ;
    	new com.globalwave.common.task.OrderSyncTask(syncControlService).run() ;
    	*/
    	new Test().test() ;

		System.out.println(Thread.currentThread().getName() + " - end action~~~");
        return null;    
    }
    
    public void prepare() throws Exception {
        
    }
    

    public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public ViewCriterion getCriterion() {
		return criterion;
	}

	public void setCriterion(ViewCriterion criterion) {
		this.criterion = criterion;
	}

	public void setViewService(ViewService viewService) {
		this.viewService = viewService;
	}

	public String getSql() {
		return sql;
	}

	public void setSql(String sql) {
		this.sql = sql;
	}

	public Long getView_id() {
		return view_id;
	}

	public void setView_id(Long view_id) {
		this.view_id = view_id;
	}

	public String getView_code() {
		return view_code;
	}

	public void setView_code(String view_code) {
		this.view_code = view_code;
	}
	
}
